package com.bsg.upm.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

	/**
	 * 
	 * @param dirPath
	 *            文件目录路径
	 * @param suffix
	 *            文件后缀名
	 * @return
	 */
	public static List<File> getFileList(String dirPath, String suffix) {
		List<File> fileList = new ArrayList<File>();
		File file = new File(dirPath);
		if (file.exists()) {
			if (file.isDirectory()) {
				File[] fileArr = file.listFiles();
				for (File f : fileArr) {
					if (f.isFile()) {
						String fileName = f.getName();
						if (fileName.substring(fileName.lastIndexOf(".")).equals(suffix)) {
							fileList.add(f);
						}
					}
				}
			}
		}
		return fileList;
	}
	
	public static String readFileByLines(String fileName) {  
        File file = new File(fileName);  
        BufferedReader reader = null;  
        StringBuffer sb = new StringBuffer();
        try {  
            reader = new BufferedReader(new FileReader(file));  
            String tempString = null;  
            while ((tempString = reader.readLine()) != null) {  
            	sb.append(tempString);
            }  
            reader.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (reader != null) {  
                try {  
                    reader.close();  
                } catch (IOException e1) {  
                }  
            }  
        } 
        return sb.toString();
    }
	
}
